package org.etom.impl.ui.wizards;

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.WorkbenchException;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.impl.ui.actions.RefreshAction;
import org.etom.impl.ui.common.ResourcesTreeViewer;
import org.etom.impl.ui.preference.PreferenceConstants;
import org.etom.impl.ui.util.Logger;
import org.etom.impl.ui.util.Template;

public class UploadWizardPage1 extends AbstractUploadWizardPage1
                               implements ISelectionChangedListener,
                                          IMenuListener {
  
  private ResourcesTreeViewer resourcesTreeViewer;
  private Template fileNameTemplate;
  
  @Override
  public void createControl(Composite parent) {
    super.createControl(parent);
    menuManager.addMenuListener(this);
    setPageComplete(false);
    initTree();
  }

  public void setFileNameTemplate(Template fileNameTemplate) {
    this.fileNameTemplate = fileNameTemplate;
  }
  
  public IMoodleModule getSelectedModule() {
    Object selection = resourcesTreeViewer.getSelection();
    if (selection instanceof IMoodleModule)
      return (IMoodleModule) selection;
    return null;
  }
  
  public String getFileName() {
    return fileNameText.getText();
  }
  
  @Override
  public void selectionChanged(SelectionChangedEvent event) {
    if (null == fileNameTemplate) return;
    ITreeSelection treeSelection = (ITreeSelection) event.getSelection();
    Object selectionElem = treeSelection.getFirstElement();
    if (selectionElem instanceof IMoodleModule) {
      fileNameText.setText(generateFileName((IMoodleModule) selectionElem));
      setPageComplete(true);
    } else {
      fileNameText.setText(""); //$NON-NLS-1$
      setPageComplete(false);
    }
  }

  /*=====[IMenuListener]======*/
  @Override
  public void menuAboutToShow(IMenuManager manager) {
    fillContextMenu(manager);
  }
  /*=====[IMenuListener]======*/
  
  private String generateFileName(IMoodleModule moodleModule) {
    fileNameTemplate.setValue(
        PreferenceConstants.TEMPLATE_KEY_ASSIGN_NAME, moodleModule.getName());
    fileNameTemplate.setValue(
        PreferenceConstants.TEMPLATE_KEY_USER_NAME,
        moodleModule.getCourse().getUser().getConnection().getUserName());
    return fileNameTemplate.toString();
  }
  
  private void initTree() {
    if (null == resourcesTreeViewer) {
      treeViewer.addSelectionChangedListener(this);
      resourcesTreeViewer = new ResourcesTreeViewer(
          treeViewer, IMoodleModule.ASSIGNMENT);
    }
    try {
      resourcesTreeViewer.load();
    } catch (WorkbenchException e) {
      Logger.ERROR(WizardsMessages.Load_MoodleConnections_Fail, e);
    }
  }
  
  /**
   * The method fills the context menu.
   */
  private void fillContextMenu(IMenuManager manager) {
    manager.add(new RefreshAction(resourcesTreeViewer));
  }
  
}
